<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/16
 * Time: 22:04
 */

class Order_model extends CI_Model
{

    public $limit_rows = 15;

    public function order_list($param = array(), $is_all = 0)
    {

        $select_sql = 'oi.*';
        if ($param['export_order'] == 1) {
            $select_sql .= ",(" . $this->db->from('order_goods AS og')->select('og.goods_name')->where("og.order_id = oi.order_id")->limit(1)->get_compiled_select() . ") AS goods_name";
            $select_sql .= ",(" . $this->db->from('order_goods AS og')->select_sum('og.goods_number')->where("og.order_id = oi.order_id")->limit(1)->get_compiled_select() . ") AS goods_number";
        }

        $this->db->select($select_sql);
        $this->db->from('order_info AS oi');

        if ($param['start_time'] && $param['end_time']) {
            $this->db->where('oi.add_time >= ', $param['start_time']);
            $this->db->where('oi.add_time <= ', $param['end_time']);
        }

        if ($param['pay_time_start'] && $param['pay_time_end']) {
            $this->db->where('oi.pay_time >= ', strtotime($param['pay_time_start']));
            $this->db->where('oi.pay_time <= ', strtotime($param['pay_time_end']));
        }

        if ($param['order_amount_start'] && $param['order_amount_end']) {
            $this->db->where('oi.order_amount >= ', $param['order_amount_start']);
            $this->db->where('oi.order_amount <= ', $param['order_amount_end']);
        }

        if ($param['shipping_name']) {
            $payment_name = array(
                1 => 'Standard Shipping(8-15 Working days)',
                2 => 'Expedited Shipping(3-7 Working Days)'
            );
            $this->db->where('oi.shipping_name', $payment_name[$param['shipping_name']]);
        }

        if($param['goods_name']) {
            $this->db->where("(oi.order_sn = '{$param['goods_name']}' OR oi.user_name LIKE '%{$param['goods_name']}%' OR oi.email LIKE '%{$param['goods_name']}%')");
        }
        
        if ($param['order_sn']) {
            
            $this->db->where("oi.order_sn in ({$param['order_sn']})");
        }

        if ($param['web_id']) {
            $this->db->where('web_id', $param['web_id']);
        }

        if ($param['deliver']) {
            $this->db->where('oi.order_status', 2);
            $this->db->where('oi.shipping_status', 0);
        } else {
            if ($param['order_status'] != -1) {
                $this->db->where('oi.order_status', $param['order_status']);
            }
    
            if ($param['shipping_status'] != -1) {
                $this->db->where('oi.shipping_status', $param['shipping_status']);
            }
        }



        if ($is_all == 0) {
            $this->db->limit($this->limit_rows,$this->limit_rows * $param['page']);
        }

        $this->db->order_by('oi.order_sn DESC,oi.order_id DESC');
        $query = $this->db->get();

        return $query->result_array();
    }

    public function order_num($param)
    {

        $this->db->from('order_info AS oi');

        if ($param['deliver']) {
            $this->db->where('oi.order_status', 2);
            $this->db->where('oi.shipping_status', 0);
        } else {
            if ($param['order_status'] != -1) {
                $this->db->where('oi.order_status', $param['order_status']);
            }
    
            if ($param['shipping_status'] != -1) {
                $this->db->where('oi.shipping_status', $param['shipping_status']);
            }
    
        }
    

        if($param['goods_name']) {
            $this->db->where("(oi.order_sn = '{$param['goods_name']}' OR oi.user_name LIKE '%{$param['goods_name']}%' OR oi.email LIKE '%{$param['goods_name']}%')");
        }

        if ($param['pay_time_start'] && $param['pay_time_end']) {
            $this->db->where('oi.pay_time >= ', strtotime($param['pay_time_start']));
            $this->db->where('oi.pay_time <= ', strtotime($param['pay_time_end']));
        }

        if ($param['order_amount_start'] && $param['order_amount_end']) {
            $this->db->where('oi.order_amount >= ', $param['order_amount_start']);
            $this->db->where('oi.order_amount <= ', $param['order_amount_end']);
        }

        if ($param['shipping_name']) {
            $this->db->where('oi.shipping_name ', $param['shipping_name']);
        }

        if ($param['web_id']) {
            $this->db->where('web_id', $param['web_id']);
        }

        return $this->db->count_all_results();
    }

    public function order_id($order_sn,$web_id)
    {
        $this->db->from('order_info');
        $this->db->where('order_sn', $order_sn);
        $this->db->where('web_id', $web_id);

        $query = $this->db->get();
        $order_info = $query->row_array();
        return $order_info['order_id'];
    }

    public function order_info($order_id)
    {
        
        $this->db->select('oi.*,sa.country_code');
        $this->db->from('order_info as oi');
        $this->db->join('shipping_address as sa', 'oi.user_id = sa.user_id', 'left');
        $this->db->where('oi.order_id', $order_id);

        $query = $this->db->get();
        $order = $query->row_array();

        return $order;
    }

    public function order_goods_num($shopify_id, $a =array())
    {
        $this->db->from('order_goods AS og');
        $this->db->join('order_info AS oi', 'og.order_id = oi.order_id', 'left');
        $this->db->join('siteweb AS pc', 'oi.web_id = pc.web_id', 'left');
        $this->db->select('SUM(og.goods_number) AS goods_number');
//        $this->db->select('SUM(og.goods_number) AS goods_number, pc.web_name,oi.web_id');
        $this->db->where('og.sku_num_sn', $shopify_id);
        $this->db->group_by('oi.web_id');

        $query = $this->db->get();
        return $query->result_array();
    }
    
    public function get_no_payment_no_order($web_id)
    {
        $query = $this->db
            ->from('order_info')
            ->select('order_id,source_id')
            ->where("payment_no = '' AND web_id = '{$web_id}'")
            ->limit(5)->order_by('order_id ASC')->get();

        $orders = $query->result_array();

        return $orders;
    }

    public function update_payment_no($data = array())
    {
        if (!$data) {
            return false;
        }

        $this->update_field(array('payment_no' => $data['payment_no']), $data['order_id']);


        $data_log = array(
            'order_id' => $data['order_id'],
            'trade_info' => $data['trade_info'],
            'log_info' => '',
            'add_time' => time()
        );
        $this->db->set($data_log);
        $this->db->insert('payment_check_log');
    }

    public function insert_order($row = array())
    {

        if (!$row['u_id']) {
            $_user = array(
                'web_user_id' => strval($row['customer']['id']),
                'email' => strval($row['customer']['email']),
                'first_name' => strval($row['customer']['first_name']),
                'last_name' => strval($row['customer']['last_name']),
                'phone' => strval($row['customer']['phone']),
                'add_time' => strtotime(strval($row['customer']['created_at'])),
                'update_time' => strval($row['customer']['created_at']),
                'web_id' => $row['web']['web_id']
            );
            $this->db->insert('customer', $_user);
            $row['u_id'] = $this->db->insert_id();
        }

        if ($row['shipping_address']) {
            $_address = array(
                'user_id' => $row['u_id'],
                'first_name' => strval($row['shipping_address']['first_name']),
                'last_name' => strval($row['shipping_address']['last_name']),
                'country' => strval($row['shipping_address']['country']),
                'province' => strval($row['shipping_address']['province']),
                'city' => strval($row['shipping_address']['city']),
                'address1' => strval($row['shipping_address']['address1']),
                'address2' => strval($row['shipping_address']['address2']),
                'zip' => strval($row['shipping_address']['zip']),
                'phone' => strval($row['shipping_address']['phone']),
                'country_code' => strval($row['shipping_address']['country_code']),
                'province_code' => strval($row['shipping_address']['province_code']),
            );
        } else {
            $_address = array(
                'user_id' => $row['u_id'],
                'first_name' => strval($row['shipping_address']['first_name']),
                'last_name' => strval($row['shipping_address']['last_name']),
                'country' => strval($row['billing_address']['country']),
                'province' => strval($row['billing_address']['province']),
                'city' => strval($row['billing_address']['city']),
                'address1' => strval($row['billing_address']['address1']),
                'address2' => strval($row['billing_address']['address2']),
                'zip' => strval($row['billing_address']['zip']),
                'phone' => strval($row['billing_address']['phone']),
                'country_code' => strval($row['billing_address']['country_code']),
                'province_code' => strval($row['billing_address']['province_code']),
            );
        }

        $shipping_query = $this->db->select('shipping_id')->from('shipping_address')->where($_address)->get();
        $shipping = $shipping_query->row_array();

        if (!$shipping) {
            $this->db->insert('shipping_address', $_address);
            $shipping['shipping_id'] = $this->db->insert_id();
        }

        $_order = array(
            'order_sn' => $row['order_number'],
            'email' => strval($row['email']),
            'order_status' => 1,
            'shipping_status' => 0,
            'pay_status' => 2,
            'subtotal_price' => floatval($row['subtotal_price']),
            'goods_amount' => floatval($row['total_line_items_price']),
            'shipping_fee' => strval($row['shipping_lines'][0]['price']),
            'order_amount' => floatval($row['total_price']),
            'discounts_amount' => floatval($row['total_discounts']),
            'total_weight' => intval($row['total_weight']),
            'currency' => strval($row['currency']),
            'shipping_name' => strval($row['shipping_lines'][0]['title']),
            'payment_name' => strval($row['gateway']),
            'add_time' => strtotime($row['created_at']),
            'pay_time' => strtotime($row['updated_at']),
            'source_id' => strval($row['id']),
            'is_audit' => 1,
            'audit_time' => $row['time'],
            'audit_id' => 0,
            'is_wrong' => 0,
            'user_id' => $row['u_id'],
            'web_id' => intval($row['web']['web_id']),
            'source_id' => strval($row['id']),
            'browser_ip' => strval($row['browser_ip']),
            'source_name' => strval($row['source_name']),
            'landing_site' => strval($row['landing_site']),
            'referring_site' => strval($row['referring_site']),
            'coupon_code'=>strval($row['discount_applications'][0]['code']),
            'shipping_id' => $shipping['shipping_id'],
            'user_name' => $_address['first_name'] . ' ' . $_address['last_name'],
            'country' => $_address['country'],
            'province' => $_address['province'],
            'city' => $_address['city'],
            'address' => $_address['address1'] . ' ' . $_address['address2'],
            'zip' => $_address['zip'],
            'phone' => $_address['phone']
        );

        $query = $this->db->insert('order_info', $_order);
        $order_id = $this->db->insert_id();

        if ($order_id && $row['line_items']) {
            $_order_goods = array();
            foreach ($row['line_items'] as $v) {
                
                $_sku_num_sn = str_replace($row['web']['ab_name'], '', strval($v['sku']));
                $_product_id = strval($v['product_id']);
                $sku_info = $row['m_goods_sku']->sku_info("gs.sku_num_sn = '$_sku_num_sn'");

                if(substr($v['sku'],0,2) == 'CW' || substr($v['sku'],0,2) == 'WW'){
                    $sku_goods_id = $this->db->from('baby_goods_sku')->where(['baby_goods_sku'=>$v['sku']])->select('id,baby_goods_id')->get()->row_array();
                    $sku_info['goods_id'] = $sku_goods_id['baby_goods_id'];
                    $sku_info['sku_id'] = $sku_goods_id['id'];
                }

                $_order_goods[] = array(
                    'order_id' => $order_id,
                    'goods_id' => intval($sku_info['goods_id']),
                    'sku_id' => intval($sku_info['sku_id']),
                    'goods_name' => strval($v['title']),
                    'sku_num_sn' => strval($v['sku']),
                    'product_id' => $_product_id,
                    'goods_number' => intval($v['quantity']),
                    'goods_price' => floatval($v['price']),
                    'goods_attr' => strval($v['variant_title']),
                    'occupy_inventory_num' => 0,
                    'erp_status' => 0,
                    'goods_weight' => floatval($v['grams']),
                    'admin_id' => 0,
                    'total_discount' => floatval($v['total_discount']),
                );
            }

            $this->db->insert_batch('order_goods', $_order_goods);

            return $order_id;
        } else {
            return false;
        }
    }

    public function check_orders_is_deliver($order_id, $log_info = '')
    {
        if ($order_id) {
            $order_number = $this->check_orders($order_id, 2);

            foreach ($order_number AS $order_id => $row) {
                $this->order_log($order_id, array(
                    'order_status' => $row['order_status'],
                    'log_info' => ($log_info ? "$log_info, 并" : '') . "订单设为可发货"
                ));
            }
        }

    }

    public function check_orders($order_id, $status_type, $log_info = '')
    {
        $data = array();
        switch ($status_type) {
            case 1:

                $query = $this->db->from('order_goods')
                    ->where("order_id = {$order_id} AND status != 4")->get();

                $order_goods = $query->row_array();

                if (!$order_goods) {
                    $data['order_status'] = 4;
                    $data['log_info'] = '取消整个订单';
                }

                break;
            case 2:

                $this->db->from('order_goods')->select('order_id, SUM(occupy_inventory_num) AS occupy_inventory_num, SUM(goods_number) AS goods_number');

                if (is_array($order_id)) {
                    $this->db->where_in('order_id', $order_id);
                    $query = $this->db->group_by('order_id')->get();
                    $is_deliver = $query->result_array();

                } else {
                    $query = $this->db->where('order_id', $order_id)->get();
                    $is_deliver = $query->row_array();

                    if ($is_deliver['occupy_inventory_num'] == $is_deliver['goods_number']) {
                        $data['order_status'] = 2;
                        $data['log_info'] = '满足发货条件，设为可发货';
                    }
                }

        }

        $data['log_info'] .= (($log_info && $data['log_info']) ? '<br />' : '') . $log_info;

        $this->order_log($order_id, $data);

        return $data;
    }
    public function order_log($order_id, $data = array()) {

        $order = $this->order_info($order_id);

        $data_order = array();

        if ($data['order_status']) {
            $order['order_status'] = $data_order['order_status'] = $data['order_status'];
        }

        if ($data['shipping_status']) {
            $order['shipping_status'] = $data_order['shipping_status'] = $data['shipping_status'];
        }

        if ($data['pay_status']) {
            $order['pay_status'] = $data_order['pay_status'] = $data['pay_status'];
        }

        if ($data_order) {
            $this->update_field($data_order, $order_id);
        }

        $data_log = array(
            'order_id' => $order_id,
            'admin_id' => intval($_SESSION['admin_id']),
            'order_status' => $order['order_status'],
            'shipping_status' => $order['shipping_status'],
            'pay_status' => $order['pay_status'],
            'log_info' => $data['log_info'],
            'add_time' => time()
        );
        $this->db->insert('order_log', $data_log);

        return true;
    }

    public function update_field($data, $order_id)
    {
        $this->db->where('order_id', $order_id);

        $this->db->update('order_info', $data);

        return true;
    }

    public function log_list($order_id)
    {
        $query = $this->db->from("order_log AS ol")
            ->select("ol.*,a.admin_name")
            ->join('admin AS a', 'ol.admin_id = a.admin_id', 'left')
            ->where('ol.order_id', $order_id)->get();

        $order_log = $query->result_array();

        return $order_log;
    }

    public function subtract_order_list()
    {
        $query = $this->db
            ->from('order_info')
            ->where("shipping_status = 0 AND order_status = 3")
            ->limit(50)->get();

        $order_list = $query->result_array();

        return $order_list;
    }

    public function order_five_sum($param){
        if(empty($param['web_id'])){
            $web_id = $this->db->from('siteweb')->select('web_id')->get()->result_array();
            $web_arr = [];
            foreach ($web_id as $key => $val) {
                $web_arr[] = $val['web_id'];
            }
            $param['web_id'] = $web_arr;
        }
        $get_web_id = implode(',',$param['web_id']);
        $current = time();
        $front = $current - 604800;
        // $arr = $this->db->query("select web_id from pcs_shopify_goods where add_time between $front and $current and goods_id = 5705")->result_array();
        // print_R($arr);die;


        $res = $this->db->from('order_goods og')
            ->select('goods_id,sum(og.goods_number) as count_number')
            ->order_by('count_number desc')
            ->join('order_info oi','og.order_id = oi.order_id', 'left')
            ->limit($this->limit_rows,$this->limit_rows * $param['page'])
            ->where("oi.add_time between $front and $current")
            ->where("oi.web_id in (select web_id from pcs_shopify_goods where add_time between $front and $current and goods_id = og.goods_id and web_id in($get_web_id))")
            ->group_by('og.goods_id')
            ->get()->result_array();
            // echo $this->db->last_query();die;
        // print_R($res);die;
        foreach ($res as $keys => $value) {
            $goods = $this->db->from('goods')->select('goods_sn,original_img')->where(['goods_id'=>$value['goods_id']])->get()->row_array();
            $res[$keys]['goods_sn'] = $goods['goods_sn'];
            $res[$keys]['original_img'] = $goods['original_img'];
            $arr = $this->db->from('shopify_goods')->select('goods_id,web_id')->where(['goods_id'=>$value['goods_id']])->group_by('web_id')->where("add_time between $front and $current")->get()->result_array();
            $web_id = array_column($arr,'web_id');
            $web_ids = [];
            foreach ($web_id as $kss => $vss) {
                foreach ($param['web_id'] as $ks => $vs) {
                    if($vss == $vs){
                        $web_ids[] = $vss;
                    }
                }
            }
            $web_sku = $this->db->from('order_goods og')
                    ->join('order_info oi','og.order_id = oi.order_id', 'left')
                    ->select('goods_attr,goods_number,web_id')
                    ->where("oi.add_time between $front and $current")
                    ->where_in("oi.web_id",$web_ids)
                    ->where('og.goods_id',$value['goods_id'])
                    ->get()->result_array();
            $web = [];
            foreach ($web_sku as $web_sku_k => $web_sku_v) {
                $web[$web_sku_v['web_id']][] = $web_sku_v;
            }
            $goods_number_web = [];
            foreach ($web as $web_sum_k => $web_sum_v) {
                $web_name = $this->db->from('siteweb')->select('web_name')->where(['web_id'=>$web_sum_k])->get()->row_array();
                $goods_number_web[$web_name['web_name']] = array_sum(array_column($web_sum_v,'goods_number'));
            }
            $res[$keys]['web_sum'] = $goods_number_web;


            $sku = [];
            foreach ($web_sku as $web_sku_k => $web_sku_v) {
                $sku[$web_sku_v['goods_attr']][] = $web_sku_v;
            }
            $goods_number_sku = [];
            foreach ($sku as $web_sum_k => $web_sum_v) {
                $goods_number_sku[$web_sum_k] = array_sum(array_column($web_sum_v,'goods_number'));
            }
            $res[$keys]['sku_sum'] = $goods_number_sku;
            $detail_views_count = 0;
            $adds_cart_count = 0;
            $item_quantity_count = 0;

            for ($i=1; $i <=6 ; $i++) {
                
                $time = strtotime(date('Y-m-d'))-(60*60*24*$i);
                $ga = $this->db->from('ga_goods')
                    ->where(['ga_date'=>$time,'goods_id'=>$value['goods_id']])
                    ->where_in('web_id',$web_ids)
                    ->get()->result_array();
                // if($value['goods_id'] == 5705){
                //     print_R($ga);die;
                //     //echo $this->db->last_query();die;
                // }
                // $res[$keys][date('Y-m-d',$time)] = $ga;
                //浏览量
                $detail_views_sum = array_sum(array_column($ga,'detail_views'));
                $res[$keys]['近'.$i.'天']['detail_views'] = $detail_views_sum;
                $detail_views_count = $detail_views_sum + $detail_views_count;
                //购物车
                $adds_cart_sum = array_sum(array_column($ga,'adds_cart'));
                $adds_cart_count = $adds_cart_count + $adds_cart_sum;
                $adds_cart_sum = $detail_views_sum == 0 ? 0 : $adds_cart_sum / $detail_views_sum * 100;
                $res[$keys]['近'.$i.'天']['adds_cart'] = substr($adds_cart_sum,0,strpos($adds_cart_sum,'.')+3).'%';
                
                //销量
                $item_quantity_sum = array_sum(array_column($ga,'item_quantity'));
                $res[$keys]['近'.$i.'天']['item_quantity'] = $item_quantity_sum;
                $item_quantity_count = $item_quantity_count + $item_quantity_sum;
                //转换率
                $item_quantity_sum = $detail_views_sum == 0 ? 0 : $item_quantity_sum / $detail_views_sum * 100;
                $res[$keys]['近'.$i.'天']['rate'] = substr($item_quantity_sum,0,strpos($item_quantity_sum,'.')+3).'%';
            }

            $res[$keys]['本星期']['detail_views_count'] = $detail_views_count;


            $adds_cart_count = $detail_views_count == 0 ? 0 : $adds_cart_count / $detail_views_count * 100;
            $res[$keys]['本星期']['adds_cart_count'] = substr($adds_cart_count,0,strpos($adds_cart_count,'.')+3).'%';

            $res[$keys]['本星期']['item_quantity_count'] = $item_quantity_count;

            $item_quantity_count = $detail_views_count == 0 ? 0 : $item_quantity_count / $detail_views_count * 100;
            $res[$keys]['本星期']['rate_count'] = substr($item_quantity_count,0,strpos($item_quantity_count,'.')+3).'%';
        }
        return $res;
    }


    public function order_five_sum_page($param){
        if(empty($param['web_id'])){
            $web_id = $this->db->from('siteweb')->select('web_id')->get()->result_array();
            $web_arr = [];
            foreach ($web_id as $key => $val) {
                $web_arr[] = $val['web_id'];
            }
            $param['web_id'] = $web_arr;
        }
        $get_web_id = implode(',',$param['web_id']);
        $current = time();
        $front = $current - 604800;
        $res = $this->db->from('order_goods og')
            ->select('goods_id,sum(og.goods_number) as count_number')
            ->order_by('count_number desc')
            ->join('order_info oi','og.order_id = oi.order_id', 'left')
            ->where("oi.add_time between $front and $current")
            ->where("oi.web_id in (select web_id from pcs_shopify_goods where add_time between $front and $current and goods_id = og.goods_id and web_id in($get_web_id))")
            ->group_by('og.goods_id');
        return $this->db->count_all_results();
    }

    public function new_goods($goods_id){
        $res = $this->db->from('new_goods')
            ->where(['goods_id'=>$goods_id,'admin_id'=>intval($_SESSION['admin_id'])])
            ->get()->row_array();
        if($res){
            return false;
        }

        $data['goods_id'] = $goods_id;
        $data['admin_id'] = intval($_SESSION['admin_id']);
        $data['add_time'] = time();
        return $this->db->insert('new_goods',$data);
    }

    public function new_goods_list($param){
        if(empty($param['admin_id'])){
            $admin_id = intval($_SESSION['admin_id']);
        }else{
            $admin_id = $param['admin_id'];
        }
        if(empty($param['web_id'])){
            $web_id = $this->db->from('siteweb')->select('web_id')->get()->result_array();
            $web_arr = [];
            foreach ($web_id as $key => $val) {
                $web_arr[] = $val['web_id'];
            }
            $param['web_id'] = $web_arr;
        }

        $res = $this->db->from('new_goods ng')
            ->select('id,ng.goods_id,sum(og.goods_number) count_number')
            ->order_by('count_number desc')
            ->join('order_goods og','og.goods_id=ng.goods_id','left')
            ->join('order_info oi','og.order_id=oi.order_id','left')
            ->where_in('oi.web_id',$param['web_id'])
            ->where(['ng.is_del'=>2])
            ->where(['ng.admin_id'=>$admin_id])
            ->limit($this->limit_rows,$this->limit_rows * $param['page'])
            ->group_by('og.goods_id')
            ->get()->result_array();

        foreach ($res as $keys => $value) {
            $goods = $this->db->from('goods')->select('goods_sn,original_img')->where(['goods_id'=>$value['goods_id']])->get()->row_array();
            $res[$keys]['goods_sn'] = $goods['goods_sn'];
            $res[$keys]['original_img'] = $goods['original_img'];
            $arr = $this->db->from('shopify_goods')->select('goods_id,web_id')->where(['goods_id'=>$value['goods_id']])->group_by('web_id')->get()->result_array();
            $web_id = array_column($arr,'web_id');
            foreach ($param['web_id'] as $k => $v) {
                if(in_array($v,$web_id)){
                    $web_ids[] = $v;
                }
            }
            $web_sku = $this->db->from('order_goods og')
                    ->join('order_info oi','og.order_id = oi.order_id', 'left')
                    ->select('goods_attr,goods_number,web_id')
                    ->where_in("oi.web_id",$web_ids)
                    ->where('og.goods_id',$value['goods_id'])
                    ->get()->result_array();
                    $web = [];
                    foreach ($web_sku as $web_sku_k => $web_sku_v) {
                        $web[$web_sku_v['web_id']][] = $web_sku_v;
                    }
                    $goods_number_web = [];
                    foreach ($web as $web_sum_k => $web_sum_v) {
                        $web_name = $this->db->from('siteweb')->select('web_name')->where(['web_id'=>$web_sum_k])->get()->row_array();
                        $goods_number_web[$web_name['web_name']] = array_sum(array_column($web_sum_v,'goods_number'));
                    }
                    $res[$keys]['web_sum'] = $goods_number_web;


                    $sku = [];
                    foreach ($web_sku as $web_sku_k => $web_sku_v) {
                        $sku[$web_sku_v['goods_attr']][] = $web_sku_v;
                    }
                    $goods_number_sku = [];
                    foreach ($sku as $web_sum_k => $web_sum_v) {
                        $goods_number_sku[$web_sum_k] = array_sum(array_column($web_sum_v,'goods_number'));
                    }
                    $res[$keys]['sku_sum'] = $goods_number_sku;
        }
        return $res;
    }

    public function new_goods_list_page($param){
        if(empty($param['admin_id'])){
            $admin_id = intval($_SESSION['admin_id']);
        }else{
            $admin_id = $param['admin_id'];
        }
        $res = $this->db->from('new_goods ng')
            ->select('ng.goods_id,sum(og.goods_number) count_number')
            ->join('order_goods og','og.goods_id=ng.goods_id','left')
            ->where(['ng.admin_id'=>$admin_id])
            ->group_by('og.goods_id');
        return $this->db->count_all_results();
    }

    public function del_goods($id){
        $info = $this->db->from('new_goods')->where(['id'=>$id,'admin_id'=>intval($_SESSION['admin_id'])])->get()->row_array();
        if(empty($info)){
            return false;
        }
        return $this->db->delete('new_goods',['id'=>$id]);
    }

    public function k_goods($_param){
        $dpd1 = strtotime($_param['dpd1']);
        $dpd2 = strtotime($_param['dpd2']);
        $goods_id = $_param['goods_id'];
        for ($i=1; $i <30 ; $i++) {

            if($dpd1 >= $dpd2){
                break;
            }
            $dpd1 =  date('Y-m-d',$dpd1);
            $date[] = $dpd1;
            $ga = $this->db->from('ga_goods')
                ->where(['ga_date'=>strtotime($dpd1),'goods_id'=>$goods_id])
                ->get()->result_array();
                // print_r($ga);die;
                
            //浏览量
            $detail_views_sum[] = array_sum(array_column($ga,'detail_views'));
            //购物车
            $adds_cart_sum[] = array_sum(array_column($ga,'adds_cart'));
            
            //销量
            $item_quantity_sum[] = array_sum(array_column($ga,'item_quantity'));

            $dpd1 = strtotime($dpd1) + 60*60*24;
        }
        $res['labels'] = $date;
        $res['detail_views_sum'] = $detail_views_sum;
        $res['adds_cart_sum'] = $adds_cart_sum;
        $res['item_quantity_sum'] = $item_quantity_sum;
        echo json_encode($res);die;
    }
}
